package com.xiaohong.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.user.dao.ChatUserMapper;
import com.xiaohong.user.pojo.User;
import com.xiaohong.user.service.ChatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ChatUserServiceImpl implements ChatUserService {

    @Autowired
    private ChatUserMapper chatUserMapper;


    /**
     * 条件+分页查询ChatUser
     * @param user 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<User> findPage(User user, int page, int size){
        // 构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>(user);
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return chatUserMapper.selectPage(pg, queryWrapper);
    }

    /**
     * 分页查询ChatUser
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<User> findPage(int page, int size){
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return chatUserMapper.selectPage(pg, null);
    }

    /**
     * 多条件搜索方法ChatUser
     * @param user
     * @return
     */
    @Override
    public List<User> findList(User user){
        // 构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>(user);
        // 查询数据
        return chatUserMapper.selectList(queryWrapper);
    }

    /**
     * 根据ID删除ChatUser
     * @param id
     */
    @Override
    public void delete(Long id){
        chatUserMapper.deleteById(id);
    }

    /**
     * 根据ID修改ChatUser
     * @param user
     */
    @Override
    public void update(User user){
        // 初始化一些不可修改数据
        user.setGmtModified(null);
        user.setGmtCreate(null);
        user.setVersion(null);
        user.setDeleted(null);
        chatUserMapper.updateById(user);
    }

    /**
     * 增加ChatUser
     * @param user
     */
    @Override
    public void add(User user){
        chatUserMapper.insert(user);
    }

    /**
     * 根据ID查询ChatUser
     * @param id
     * @return
     */
    @Override
    public User findById(Long id){
        return  chatUserMapper.selectById(id);
    }

    /**
     * 查询全部ChatUser数据
     * @return
     */
    @Override
    public List<User> findAll() {
        return chatUserMapper.selectList(null);
    }

    /**
     * 关键字搜索
     * @param key
     * @return
     */
    @Override
    public List<User> SearchUser(String key) {
        List<User> list = new ArrayList<User>();
        // 构建查询条件
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<User>();
        // 全等于id
        List<User> list1 = chatUserMapper.selectList(queryWrapper1.eq("id",key));
        if (list1.size() > 0){
            if(!isContain(list,list1.get(0).getId())){
                list.add(list1.get(0));
            }
        }

        QueryWrapper<User> queryWrapper2 = new QueryWrapper<User>();
        // 模糊等于id
        List<User> list2 = chatUserMapper.selectList(queryWrapper2.like("id",key));
        if (list2.size() > 0){
            for (User user : list2) {
                if(!isContain(list,user.getId())){
                    list.add(user);
                }
            }
        }

        QueryWrapper<User> queryWrapper3 = new QueryWrapper<User>();
        // 全等于昵称
        List<User> list3 = chatUserMapper.selectList(queryWrapper3.eq("nickName",key));
        if (list3.size() > 0){
            for (User user : list3) {
                if(!isContain(list,user.getId())){
                    list.add(user);
                }
            }
        }

        QueryWrapper<User> queryWrapper4 = new QueryWrapper<User>();
        // 模糊等于昵称
        List<User> list4 = chatUserMapper.selectList(queryWrapper4.like("nickName",key));
        if (list4.size() > 0){
            for (User user : list4) {
                if(!isContain(list,user.getId())){
                    list.add(user);
                }
            }
        }

        // 返回查询数据
        return list;
    }

    public Boolean isContain(List<User> list, Long id){
        Boolean flag = false;
        for (User user : list) {
            if (id.equals(user.getId())){
                flag = true;
            }
        }
        return flag;
    }

}
